#  Copyright (c) 2020 - ~, WANGXI
#  
#  TIMER TEST TCL FILE
#  Used in NS-2 simulator
#  Clone from https://gitee.com/WangXi_Chn/ns2_modulelib
#  Note in https://www.yuque.com/wangxi_chn/kozrfl
#  
#  Change Logs:
#  Date           Author       Notes			Mail
#  2020-12-03     WangXi   	   first version	WangXi_Chn@foxmail.com

# Time class define
Class Timer

# Time class example process and achieve
Timer instproc sched delay {
    global ns
    $self instvar id_
    $self cancel
    set id_ [$ns at [expr [$ns now]+$delay]"$self timeout"]
}

# destroy timer example process
Timer instproc destroy {} {
    $self cancel
}

# cancel timer example process
Timer instproc cancel {} {
    global ns
    $self instvar id_
    if {[info exists id_]} {
        $ns cancel $id_
        unset id_
    }
}

# reschedule timer example process
Timer instproc resched delay {
    $self sched $delay
}

# children class must provide timeout process
Timer instproc expire {} {
    $self timeout
}

# timeout process example process
Timer instproc timeout {} {
    $self instvar ns
    global f
    set ns [Simulator instance]
    puts "timeout..."
    puts $f "Simulation time is [$ns now]..."
    $self sched 200000
}


set f [open testfile w]
set ns [new Simulator]
set mt_ [new Timer]
$mt_ sched 10
$ns run


#/************************ (C) COPYRIGHT 2020 WANGXI **************END OF FILE****/
